import { getRouteItemByMenuItem } from "@utils/router";
import service from "@/service";
import { Message } from "element-ui";
import { getTokenInfo } from "@utils/session";


// 菜单处理
export const handleList = [
    {   //订单中心
        match(menu = {}) {
            console.log("订单中心菜单特殊处理:",menu);
            let url = menu.ELEMENTURL || ""
            if(url.startsWith('/ThirdPartyLogin/orderInfo')) {
                return true;
            }
        },
        /**
         * 
         * @param {object} menu 配置的菜单对象
         * @param {function} breakOut 调用之后，后面的菜单处理逻辑不会再执行了
         */
        async handler(menu, breakOut) {
            breakOut();
            let threeSystemUrl =''
            console.log("进入订单中心菜单");
            let result = await service('orderInfo')
            .then(
                result => {
                            if(result?.data?.url) {
                                threeSystemUrl= result.data.url;
                                window.open(threeSystemUrl)
                            }else{
                                Message.error("无法获取订单中心链接");
                                return;
                            }
                }
            )
            .catch(e => {
                Message.error("调用订单中心接口失败");
                return;   
            })
        }
    },
    {   //沃地图
        match(menu = {}) {
            let tokenInfo = getTokenInfo();
            console.log("沃地图菜单特殊处理1:",tokenInfo);
            console.log("沃地图菜单特殊处理:",menu);
            let url = menu.ELEMENTURL || ""
            if(url.startsWith('/ThirdPartyLogin/mapInfo')) {
                return true;
            }
        },
        /**
         * 
         * @param {object} menu 配置的菜单对象
         * @param {function} breakOut 调用之后，后面的菜单处理逻辑不会再执行了
         */
        async handler(menu, breakOut) {
            breakOut();     
            const serialNumber = window.g_instance?.$store?.state?.account?.accountObject?.mobilePhone;
            console.log("进入沃地图获取手机号码:",serialNumber);
            if(!serialNumber){
                Message.error("无法获取登录员工手机号码,请您核查");
                return;            
            }
            let threeSystemUrl =''
            let result = await service('mapInfo', {
                data: {
                    staffid: serialNumber,//'18698065423'  丽雅手机号客户看效果
                  },
            }).then(
                result => {
                    if(result?.data?.resCode){
                        if(result?.data?.resCode == 200){
                            if(result?.data?.resData?.url) {
                                threeSystemUrl= result.data.resData.url;
                                window.open(threeSystemUrl)
                            }else{
                                Message.error("无法获取沃地图链接");
                                return;
                            }
                        }else{
                            Message.error("调用沃地图报错"+result?.data?.message);
                            return;
                        }

                    }

                }
            ).catch(e => {
                Message.error("调用沃地图接口失败");
                return;    
            })
        }
    }, 
    {   //会员中心
        match(menu = {}) {
            console.log("会员中心菜单特殊处理:",menu);
            let url = menu.ELEMENTURL || ""
            if(url.startsWith('/ThirdPartyLogin/vipInfo')) {
                return true;
            }
        },
        /**
         * 
         * @param {object} menu 配置的菜单对象
         * @param {function} breakOut 调用之后，后面的菜单处理逻辑不会再执行了
         */
        async handler(menu, breakOut) {
            breakOut();     
            const staffId = window.g_instance?.$store?.state?.account?.accountObject?.staffId;
            console.log("进入会员中心获取工号:",staffId);
            if(!staffId){
                Message.error("无法获取登录员工工号,请您核查");
                return;            
            }
            let threeSystemUrl =''
            let result = await service('vipInfo', {
                data: {
                    staffid: staffId 
                    },
            }).then(
                result => {
                    if(result?.data?.url) {
                        threeSystemUrl= result.data.url;
                        window.open(threeSystemUrl)
                    }else{
                        Message.error("无法获取会员中心链接");
                        return;
                    }

                }
            ).catch(e => {
                Message.error("调用会员中心接口失败");
                return;    
            })
        }
    }, 
    {   //CBSS
        match(menu = {}) {
            console.log("CBSS菜单特殊处理:",menu);
            let url = menu.ELEMENTURL || ""
            if(url.startsWith('/ThirdPartyLogin/bssframe')) {
                return true;
            }
        },
        /**
         * 
         * @param {object} menu 配置的菜单对象
         * @param {function} breakOut 调用之后，后面的菜单处理逻辑不会再执行了
         */
        async handler(menu, breakOut) {
            breakOut();     
            const staffId = window.g_instance?.$store?.state?.account?.accountObject?.staffId;
            console.log("进入CBSS获取工号:",staffId);
            if(!staffId){
                Message.error("无法获取登录员工工号,请您核查");
                return;            
            }
            let tokenInfo = getTokenInfo();
            console.log("CBSS菜单特殊处理:",tokenInfo);
            
            let threeSystemUrl =''
            let result = await service('bssframe', {
                data: {
                    staffid: staffId ,
                    session_id: tokenInfo.access_token
                    },
            }).then(
                result => {
                    if(result?.data?.url) {
                        threeSystemUrl= result.data.url;
                        window.open(threeSystemUrl)
                    }else{
                        Message.error("无法获取CBSS链接");
                        return;
                    }

                }
            ).catch(e => {
                Message.error("调用CBSS接口失败");
                return;    
            })
        }
    }, 
    {
        match() {
            return true;
        },
        handler(menu, breakOut, navManage) {
            let routeSet = getRouteItemByMenuItem(menu);

            if (routeSet.name === "iframe") {
                const { path, moduleName, iframeUrl } =
                    g_instance.$route.query || {};

                // if(moduleName) {
                //     if(iframeUrl === active.iframeUrl) {
                //         return;
                //     }
                // }else
                if (path === menu.path) {
                    return;
                }
            } else if (g_instance.$route.path === routeSet.path) {
                return;
            }
            if(menu.OPENMODULE === 'Y') {
                // 新窗口打开
                if(routeSet.name === "iframe") {
                    window.open(routeSet.query.path);
                }else {
                    let href = g_instance.$router.resolve({
                        name: routeSet.name
                    })?.href; 
                    if(href) {
                        let url = `${location.origin}${location.pathname}${href}?fullScreen=1`
                        window.open(url);
                    }
                }
            }else {
                // 主窗口打开
                navManage.jumpTo(routeSet);
            }
        }
    }
];